﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace Embedded_Sensor_Cloud
{
    public class Response
    {
         //Attribute
        private Socket socket;

        //Properties
        public int StatusCode { get; set; }
        public string StatusText { get; set; }
        //public int ContentLength { get; set; }
        public string ContentType { get; set; }
        public string Data { get; set; }

        public Response(Socket socket)
        {
            this.socket = socket;
        }

        public bool sendResponse()
        {
            try
            {
                NetworkStream stream = new NetworkStream(socket);
                StreamWriter writer = new StreamWriter(stream);

                writer.WriteLine("HTTP/1.0 " + StatusCode + " " + StatusText);
                writer.WriteLine("Date: " + DateTime.Now.ToString());
                writer.WriteLine("Server: Embedded_Sensor_Cloud Webserver v0.1");
                writer.WriteLine("Content-Length: " + Data.Length);
                writer.WriteLine("Connection: close");
                writer.WriteLine("Content-Type: " + ContentType);
                writer.WriteLine();

                writer.WriteLine(Data);
                writer.Flush();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
                return false;
            }

            return true;
        }
    }
}
